<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%String path = request.getContextPath();%>
<html>
<head>
    <title>Title</title>
    <link href="<%=path%>/static/layui/css/layui.css" rel="stylesheet">
    <link href="<%=path%>/static/zTree_v3/css/zTreeStyle/zTreeStyle.css" type="text/css" rel="stylesheet">
</head>
<body>
<div class="layui-fluid" style="margin-top: 15px;">
    <div class="layui-card" style="border-radius:20px;box-shadow: 10px 10px 5px #cccccc;padding: 5px;">
        <div class="layui-card-body">
            <span class="pzright"
                  style="display: flex;justify-content: flex-start;margin: 5px;">
                <p class="xm-d1-p2">
                    <button class="layui-btn layui-btn-sm" id="btn-expand"
                            style="background-color: #1AB394;color: white;"
                            onclick="myTree.expandAll(true)">全部展开</button>
                    <button class="layui-btn layui-btn-sm" id="btn-fold"
                            style="background-color: #1AB394;color: white;"
                            onclick="myTree.expandAll(false)">全部折叠</button>
               </p>
            </span>
            <b>组织结构</b>
            <ul id="menuTree" class="ztree" style="width:230px; overflow:auto;"></ul>
        </div>
    </div>
</div>
<script src="<%=path%>/static/layui/layui.all.js"></script>
<script src="<%=path%>/static/js/jquery.min.js?v=2.1.4"></script>
<script src="<%=path%>/static/zTree_v3/js/jquery.ztree.all.min.js"></script>
<script>
    var choosedNodeId
    var layer=window.top.layer
    function chooseNode(event, treeId, treeNode) {
        alert(treeNode.tId + ", " + treeNode.name);
    }

    var setting = {
        async: {
            enable: true,
            url: "<%=path%>/system/menu/list",
            dataFilter: ajaxDataFilter
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "parentId",
                rootPId: 0
            }
        },

        callback : {//回调函数
            beforeClick: zTreeBeforeClick,
            onAsyncSuccess: zTreeOnAsyncSuccess,// 异步加载正常结束的事件回调函数

        },
    };
    function ajaxDataFilter(treId, parentNode, responseData) {
        return responseData?.data instanceof Array?responseData.data:[];
    };
    function zTreeOnAsyncSuccess(){
        let parentId=iframeObj.preWindow.$("#parentIdValue").val()
    let parentNode = myTree.getNodeByParam("id", parentId, null)
        myTree.selectNode(parentNode);
        myTree.expandAll(true);
    }
    function zTreeBeforeClick(treeId, treeNode, clickFlag) {
        let thisId=iframeObj.preWindow.$("#id").val()
        if(treeNode.id == thisId){
            layer.msg("不能选择自身为父节点",{
                zIndex: layer.zIndex
            })
            return false
        }
    }

    //初始化
    window.myTree = $.fn.zTree.init($("#menuTree"), setting, null);


</script>
</body>
</html>
